package com.ihd.ihardware.view.tzc.discovery.view;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.ihd.ihardware.R;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.databinding.ActivityDtBinding;
import com.ihd.ihardware.pojo.EmptyRes;
import com.ihd.ihardware.pojo.FileRes;
import com.ihd.ihardware.view.tzc.discovery.viewmodel.DTViewModel;

import java.io.File;
import java.util.Arrays;

import cn.wowjoy.commonlibrary.adapter.DialogListAdapter;
import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.utils.DialogUtils;
import cn.wowjoy.commonlibrary.utils.GlideRoundTransform;
import cn.wowjoy.commonlibrary.utils.ImageUtils;
import cn.wowjoy.commonlibrary.utils.InputTools;
import cn.wowjoy.commonlibrary.utils.ToastUtils;
import cn.wowjoy.commonlibrary.widget.MDialog;

public class DTActivity extends BaseActivity<ActivityDtBinding, DTViewModel> {
    public static final int PHOTO_PICKED_WITH_PATH = 3022;
    public static final int CAMERA_WITH_DATA = 3023;
    private MDialog mBottomDG;
    private String avatarId;
    private MDialog mPublicDG;

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(true).init();
        binding.mtitlebar.setTitle("发布动态");
        binding.mtitlebar.setLeftImageResource(R.drawable.ic_back);
        binding.mtitlebar.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputTools.HideKeyboard(binding.sayET);
                finish();
            }
        });

        binding.sayET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(avatarId) && s.length() > 0)
                    binding.publishTV.setEnabled(true);
                else
                    binding.publishTV.setEnabled(false);
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        binding.sayIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBottomDG = DialogUtils.BottomListDialog(DTActivity.this, Arrays.asList("拍照", "从相册中选择"), new DialogListAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, String data) {
                        mBottomDG.cancel();
                        if (data.equals("拍照")) {
                            ToastUtils.showLong(DTActivity.this, AppConstans.BASE_PATH);
                            Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            Uri imguri = Uri.fromFile(new File(AppConstans.BASE_PATH + AppConstans.HEAD_PATH));
                            openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imguri);
                            startActivityForResult(openCameraIntent, CAMERA_WITH_DATA);
                        } else {
                            Intent intent = new Intent(Intent.ACTION_PICK);
                            intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                            startActivityForResult(intent, PHOTO_PICKED_WITH_PATH);
                        }
                    }
                });
            }
        });

        binding.publishTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.sign(avatarId, binding.sayET.getText().toString());
            }
        });

        setRx(AppConstans.UPDATE, new BaseConsumer<FileRes>() {
            @Override
            protected void onStart() {
                DialogUtils.waitingDialog(DTActivity.this);
            }

            @Override
            protected void onSuccess(FileRes mLiveData) {
                DialogUtils.dismiss(DTActivity.this);
                Glide.with(getApplicationContext())
                        .load(Uri.parse(mLiveData.getData().getUrl()))
                        .apply(new RequestOptions().transform(new GlideRoundTransform(5)))
                        .into(binding.sayIV);
                avatarId = mLiveData.getData().getId();
                if (binding.sayET.getText().length() > 0)
                    binding.publishTV.setEnabled(true);
                else
                    binding.publishTV.setEnabled(false);
            }

            @Override
            protected void onError(Throwable error) {
                ToastUtils.showShort(DTActivity.this, error.getMessage());
                DialogUtils.dismiss(DTActivity.this);
            }
        });

        setRx(AppConstans.PUBLISH, new BaseConsumer<EmptyRes>() {
            @Override
            protected void onStart() {
                mPublicDG = DialogUtils.publishDialog(DTActivity.this);
            }

            @Override
            protected void onSuccess(EmptyRes mLiveData) {
                mPublicDG.cancel();
                finish();
            }

            @Override
            protected void onError(Throwable error) {
                ToastUtils.showShort(DTActivity.this, error.getMessage());
                mPublicDG.cancel();
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_dt;
    }

    @Override
    protected Class getViewModelClass() {
        return DTViewModel.class;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            String path = AppConstans.BASE_PATH + AppConstans.HEAD_PATH_S;
            switch (requestCode) {
                case CAMERA_WITH_DATA:
                    File f = ImageUtils.zoomImageFile(AppConstans.BASE_PATH + AppConstans.HEAD_PATH, path, 1080);
                    if (f != null)
                        viewModel.updateHead(f);
                    break;
                case PHOTO_PICKED_WITH_PATH:
                    final Uri uri = data.getData();
                    if (uri != null) {
                        File f0 = ImageUtils.zoomImageFile(ImageUtils.uri2path(this, uri), path, 1080);
                        if (f0 != null)
                            viewModel.updateHead(f0);
                    }
                    break;
            }
        }
    }
}
